package com.wanmait.recycle.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wanmait.recycle.pojo.Data;
import com.wanmait.recycle.pojo.Rubbish;

public class CalculateDataUtil {
	private CalculateDataUtil(){};
	private static CalculateDataUtil c=new CalculateDataUtil();
	public static CalculateDataUtil getInstance() {
		return c;
	}
	//以吨为单位
	public static double getCount(String beginTime,String endTime,List<Data> allDataToRubbishAndCity) {
		long beginTimeMills=0;
		long endTimeMills=0;
		double sum=0;
		double[] unit= { 0.000001,0.001,1,0.001,0.0005};
		
		Calendar c = Calendar.getInstance();
		try {
			c.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(beginTime));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		beginTimeMills=c.getTimeInMillis();
		try {
			c.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(endTime));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		endTimeMills=c.getTimeInMillis();
		
		for(Data data:allDataToRubbishAndCity) {
			
			Long thisBeginMills=data.getBeginTime().getTime();
			Long thisEndMills=data.getEndTime().getTime();
			if(thisBeginMills>=endTimeMills ||thisEndMills<=beginTimeMills) {			
			}
			else if(thisBeginMills>=beginTimeMills && thisEndMills<=endTimeMills){
				
				sum+=data.getCount()*unit[data.getUnit().getId()-1];
			}
			else if(thisBeginMills>=beginTimeMills){
				Long effectperiod=endTimeMills-thisBeginMills;
				
				sum+=(effectperiod*1.0/(thisEndMills-thisBeginMills))*data.getCount()*unit[data.getUnit().getId()-1];
			}
			else {
				Long effectperiod=thisEndMills-beginTimeMills;
				sum+=(effectperiod*1.0/(thisEndMills-thisBeginMills))*data.getCount()*unit[data.getUnit().getId()-1];
			}
			
		}
		
		return sum;
	}
	public Map<String, Double> dataStatistics(Integer nRubbishId, Integer nCityId, String beginTime, String endTime,List<Data>allDatas){
		
		double disposeCount=0;
		double dischargeCount=0;
		List<Data> datasDischarge=new ArrayList<Data>();
		List<Data> datasDispose=new ArrayList<Data>();
		/*
		 * if(nCityId==-1) {
		 * datasDischarge=dataMapper.getNationalDatasConcern(rubbish,false);
		 * 
		 * datasDispose=dataMapper.getNationalDatasConcern(rubbish,true); } else {
		 * datasDischarge=dataMapper.getDatasConcern(rubbish,nCityId,false);
		 * 
		 * datasDispose=dataMapper.getDatasConcern(rubbish,nCityId,true); }
		 */
		for(Data data:allDatas) {
			if(nCityId==-1) {
				if(data.getIsDispose()==true) {
					datasDispose.add(data);
					}
				else {
					datasDischarge.add(data);
				}
				continue;
			}
			else {
				if(data.getRubbish().getId()==nRubbishId&&(long)nCityId==data.getCity().getCity_ID()) {
					if(data.getIsDispose()==true) {
						datasDispose.add(data);
					}
					else {
						datasDischarge.add(data);
					}
				}
			}
		}
		disposeCount=CalculateDataUtil.getCount(beginTime, endTime, datasDispose);
		dischargeCount=CalculateDataUtil.getCount(beginTime, endTime, datasDischarge);
	
		Map<String, Double> map=new HashMap<>();
		map.put("disposeCount",disposeCount);
		map.put("dischargeCount",dischargeCount);
		return map;
	}
}
